<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Grurrah" Inherits="Grr.Web.GrurrahPage" ValidateRequest="false" %>

<%@ Register Src="controls/CommentsControll.ascx" TagName="CommentsControll" TagPrefix="uc2" %>

<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>

<%@ Register Src="controls/GrurrahVacantControl.ascx" TagName="GrurrahVacantControl" TagPrefix="uc1" %>
<%@ Register Src="controls/GrurrahCreateControl.ascx" TagName="GrurrahCreateControl" TagPrefix="uc" %>
<%@ Register Src="controls/GrurrahEditControl.ascx" TagName="GrurrahEditControl" TagPrefix="uc" %>
<%@ Register Src="controls/GrurrahReadControl.ascx" TagName="GrurrahReadControl" TagPrefix="uc" %>
<%@ Register Src="controls/RatingControl.ascx" TagName="RatingControl" TagPrefix="uc" %>
<%@ Reference Control="~/controls/HUDControl.ascx"  %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Grr.Web" %>
<%@ Import Namespace="Grr.Model" %>

<script runat="server">

    private static readonly string EmptyTagsListText = " click here to add tags ";
    public override void OnLoginStatusChanged(GrurrahUser user, bool auth)
    {
        base.OnLoginStatusChanged(user, auth);

        GrurrahReadControl.OnLoginStatusChanged(user, auth);
        GrurrahEditControl.OnLoginStatusChanged(user, auth);
        GrurrahCreateControl.OnLoginStatusChanged(user, auth);
        GrurrahVacantControl.OnLoginStatusChanged(user, auth);
    }
    
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!IsPostBack)
        {
            Grurrah g = (Grurrah)Session["CurrentGrurrah"];

            if (g != null)
            {
                Title = g.Name;
                UpdateTags();
            }
        }
    }

    protected void TagsList_TextChanged(object sender, EventArgs e)
    {
        string[] tags = TagsList.Text.Split(new char[] { ',' });
        Grurrah g = (Grurrah)Session["CurrentGrurrah"];

        System.Threading.ThreadPool.QueueUserWorkItem(delegate(object data)
        {
            Grurrah grurrah = (Grurrah)data;
            grurrah.SetTags(tags);
        }, g);

        if (TagsList.Text.Trim () == String.Empty) {
            TagsList.Text = EmptyTagsListText;
        }
    }
        
    protected override void SwitchToEditMode()
    {
        base.SwitchToEditMode();

        GrurrahEditPanel.Visible = true;
        GrurrahControlsPanel.Visible = true;
        GrurrahEditControl.UpdateGrurrahElements();
        UpdateTags();
        
        GrurrahReadPanel.Visible = false;
        GrurrahCreatePanel.Visible = false;
        GrurrahVacantPanel.Visible = false;
    }

    protected override void SwitchToReadMode()
    {
        base.SwitchToReadMode();

        GrurrahReadPanel.Visible = true;
        GrurrahControlsPanel.Visible = true;
        GrurrahReadControl.UpdateGrurrahElements();
        UpdateTags();
        
        GrurrahEditPanel.Visible = false;
        GrurrahCreatePanel.Visible = false;
        GrurrahVacantPanel.Visible = false;

        GrurrahReadPanel.ForceAnUpdate();
    }

    protected override void SwitchToCreateMode()
    {
        base.SwitchToCreateMode();

        GrurrahCreatePanel.Visible = true;
        GrurrahControlsPanel.Visible = false;
        GrurrahEditPanel.Visible = false;
        GrurrahReadPanel.Visible = false;
        GrurrahVacantPanel.Visible = false;
    }

    protected override void SwitchToVacantMode()
    {
        base.SwitchToVacantMode();

        GrurrahVacantPanel.Visible = true;
        GrurrahControlsPanel.Visible = false;
        GrurrahCreatePanel.Visible = false;
        GrurrahEditPanel.Visible = false;
        GrurrahReadPanel.Visible = false;
    }

    private void UpdateTags()
    {
        Grurrah g = (Grurrah)Session["CurrentGrurrah"];
        
        if (TagsList.Text.Length < 1)
        {
            StringBuilder tag_text = new StringBuilder();
            for (int i = 0; i < g.Tags.Length; i++)
            {
                tag_text.Append(g.Tags[i]);
                if (i != g.Tags.Length - 1)
                    tag_text.Append(", ");
            }

            if (g.Tags.Length == 0)
                TagsList.Text = EmptyTagsListText;
            else
                TagsList.Text = tag_text.ToString();
        }
    }

</script>
    

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
    <script type="text/javascript">addthis_pub  = 'grurrah';</script>
    <script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>
    
    <script language="javascript">
    
        function is_rating_locked ()
        {
            var cont = document.getElementById ("rating_locked_container");
            var list = cont.getElementsByTagName ("input");
            
            if (list [0] != null)
                return list [0].value == "true";
            return false;
        }
        
        function get_rating_average ()
        {
            var cont = document.getElementById ("rating_avg_container");
            var list = cont.getElementsByTagName ("input");
            
            if (list [0] != null)
                return list [0].value;
            return 0;
        }
        
        function ratings_clear ()
        {
            if (is_rating_locked ()) {
                set_cursor_for_stars (false);
                return;
            }

            set_cursor_for_stars (true);
                
            var i;
            var rating_average = get_rating_average ();
            var stars = document.getElementsByClassName("RatingsStar")
           
            for (i = 0; i < rating_average; i++)
                stars [i].src = "/media/rating-star-full.gif";
            
            for ( ; i < 5; i++)
                stars [i].src = "/media/rating-star-empty.gif";
        }

        function ratings_set (rating)
        {
            if (is_rating_locked ()) {
                set_cursor_for_stars (false);
                return;
            }

            set_cursor_for_stars (true);

            var i;
            var stars = document.getElementsByClassName("RatingsStar");
           
            for (i = 0; i < rating; i++)
                stars [i].src = "/media/rating-star-full.gif";
            
            for ( ; i < 5; i++)
                stars [i].src = "/media/rating-star-empty.gif";
                
            rating_average = rating;
        }

        function set_cursor_for_stars (enabled)
        {
            var stars = document.getElementsByClassName("RatingsStar");
            if (enabled) {
                for (var i = 0; i < 5; i++) {
                    stars [i].style.cursor = "pointer";
                }
            } else {
                for (var i = 0; i < 5; i++)
                    stars [i].style.cursor = "default";
            }
        }
    </script>

    <gaia:Panel ID="GrurrahPanel" runat="server">
        <gaia:Panel ID="GrurrahVacantPanel" Visible="false" runat="server">
            <uc1:GrurrahVacantControl ID="GrurrahVacantControl" runat="server" />
        </gaia:Panel>
        <gaia:Panel ID="GrurrahEditPanel" Visible="false" runat="server">
            <uc:GrurrahEditControl ID="GrurrahEditControl" runat="server" />
        </gaia:Panel>
        <gaia:Panel ID="GrurrahCreatePanel" Visible="false" runat="server">
            <uc:GrurrahCreateControl ID="GrurrahCreateControl" runat="server" />
        </gaia:Panel>
        <gaia:Panel ID="GrurrahReadPanel" runat="server">
            <uc:GrurrahReadControl ID="GrurrahReadControl" runat="server" />
        </gaia:Panel>             
    </gaia:Panel>
    <gaia:Panel ID="GrurrahControlsPanel" runat="server">
        <table width="500" style="margin: 15px 15px 15px 15px">
            <tr>
                <td valign="top" style="width: 38px">
                    Rate:</td>
                <td>
                    <uc:RatingControl id="RatingControl1" runat="server" RatingCount="5" RatingTotal="25" />
                </td>
            </tr>
            <tr>
                <td valign="top" style="width: 38px">
                    Tags:</td>
                <td>
                    <gaia:InPlaceEdit ID="TagsList" runat="server" SingleLine="true" CssClass="tags_list" 
                        OnTextChanged="TagsList_TextChanged"></gaia:InPlaceEdit>
                </td>
            </tr>
            <tr>
                <td style="width: 38px; height: 35px;" valign="top">
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td colspan="2" valign="top">
                    <uc2:CommentsControll ID="CommentsControll1" runat="server" />
                </td>
            </tr>
            
        </table>
    </gaia:Panel>
</asp:Content>

